package com.ksc.wordcount.util;

import javafx.beans.property.Property;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class PropertyUtil {

    private static String projectPath = System.getProperty("user.dir");
    private static String fileSeparator = System.getProperty("file.separator");


    public static List<Map<String,String>> getMasterProperties() {
        String configFilePath = projectPath + fileSeparator + "master.conf";
        System.out.println(configFilePath);
        List res=new ArrayList();
        try (BufferedReader br = new BufferedReader(new FileReader(configFilePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                // 跳过空行和注释行
                if (line.trim().isEmpty() || line.trim().startsWith("#")) {
                    continue;
                }

                // 按空格或制表符分隔数据字段
                String[] fields = line.split("\\s+");

                // 获取每个字段的值
                String ip = fields[0];
                String akkaPort = fields[1];
                String thriftPort = fields[2];
                String memory = fields[3];

                Map<String,String> property=new HashMap<>();
                property.put("ip",ip);
                property.put("akkaPort",akkaPort);
                property.put("thriftPort",thriftPort);
                property.put("memory",memory);
                res.add(property);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static Map<String,String> getUrltopnProperties() {
        String configFilePath = projectPath + fileSeparator + "urltopn.conf";
        System.out.println(configFilePath);
        Map<String,String> property=new HashMap<>();
        try (BufferedReader br = new BufferedReader(new FileReader(configFilePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                // 跳过空行和注释行
                if (line.trim().isEmpty() || line.trim().startsWith("#")) {
                    continue;
                }

                // 按空格或制表符分隔数据字段
                String[] fields = line.split("\\s+");

                // 获取每个字段的值
                String applicationId  = fields[0];
                String inputPath = fields[1];
                String outputPath = fields[2];
                String topN = fields[3];
                String reduceTask=fields[4];
                String splitSize=fields[5];

                property.put("applicationId",applicationId);
                property.put("inputPath",inputPath);
                property.put("outputPath",outputPath);
                property.put("topN",topN);
                property.put("reduceTask",reduceTask);
                property.put("splitSize",splitSize);

//                System.out.println(applicationId);
//                System.out.println(inputPath);
//                System.out.println(outputPath);
//                System.out.println(topN);
//                System.out.println(reduceTask);
//                System.out.println(splitSize);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return property;
    }

    public static void main(String[] args) {


    }
}
